<?php
App::uses('AppController', 'Controller');

class UsersController extends AppController {

	public $paginate = array('limit' => 25, 'conditions' => array('status' => '1'), 'order' => array('User.username' => 'asc'));

	public function beforeFilter() {
		parent::beforeFilter();
		//$this -> Auth -> allow('login', 'add');
	}

	// public function index() {
		// $conditions = array("User.active" => 1);
		// $this -> paginate = array('limit' => ITEM_PER_PAGE, 'order' => array('User.username' => 'asc'));
		// $users = $this -> paginate('User',$conditions);
		// //$this -> set(compact('users'));
		// echo "<pre>";
		// print_r($users);exit();
		// $this->set("data",$users);
	// }
// 
	// public function add() {
		// if ($this -> request -> is('post')) {
			// $this -> User -> create();
			// if ($this -> User -> save($this -> request -> data)) {
				// $this -> Session -> setFlash(__('The user has been created'));
				// $this -> redirect(array('action' => 'index'));
			// } else {
				// $this -> Session -> setFlash(__('The user could not be created. Please, try again.'));
			// }
		// }
	// }
// 
	// public function edit($id = null) {
// 
		// if (!$id) {
			// $this -> Session -> setFlash('Please provide a user id');
			// $this -> redirect(array('action' => 'index'));
		// }
// 
		// $user = $this -> User -> findById($id);
		// if (!$user) {
			// $this -> Session -> setFlash('Invalid User ID Provided');
			// $this -> redirect(array('action' => 'index'));
		// }
// 
		// if ($this -> request -> is('post') || $this -> request -> is('put')) {
			// $this -> User -> id = $id;
			// if ($this -> User -> save($this -> request -> data)) {
				// $this -> Session -> setFlash(__('The user has been updated'));
				// $this -> redirect(array('action' => 'edit', $id));
			// } else {
				// $this -> Session -> setFlash(__('Unable to update your user.'));
			// }
		// }
// 
		// if (!$this -> request -> data) {
			// $this -> request -> data = $user;
		// }
	// }
// 
	// public function delete($id = null) {
		// if (!$id) {
			// $this -> Session -> setFlash('Please provide a user id');
			// $this -> redirect(array('action' => 'index'));
		// }
// 
		// $this -> User -> id = $id;
		// if (!$this -> User -> exists()) {
			// $this -> Session -> setFlash('Invalid user id provided');
			// $this -> redirect(array('action' => 'index'));
		// }
		// if ($this -> User -> saveField('status', 0)) {
			// $this -> Session -> setFlash(__('User deleted'));
			// $this -> redirect(array('action' => 'index'));
		// }
		// $this -> Session -> setFlash(__('User was not deleted'));
		// $this -> redirect(array('action' => 'index'));
	// }
// 
	// public function activate($id = null) {
		// if (!$id) {
			// $this -> Session -> setFlash('Please provide a user id');
			// $this -> redirect(array('action' => 'index'));
		// }
// 
		// $this -> User -> id = $id;
		// if (!$this -> User -> exists()) {
			// $this -> Session -> setFlash('Invalid user id provided');
			// $this -> redirect(array('action' => 'index'));
		// }
		// if ($this -> User -> saveField('status', 1)) {
			// $this -> Session -> setFlash(__('User re-activated'));
			// $this -> redirect(array('action' => 'index'));
		// }
		// $this -> Session -> setFlash(__('User was not re-activated'));
		// $this -> redirect(array('action' => 'index'));
	// }

	/**
	* admin index method
	*/
	public function admin_index() {
		$conditions = array("User.active" => 1);
		$this->paginate = array(
                                'limit' => 2,
                                'order' => array('id' => 'asc'),
                             );
        $data = $this->paginate("User",$conditions);
        $this->set("data",$data);
	}
	/**
	* admin add method
	*/
	public function admin_add(){
		if ($this -> request -> is('post')) {
			$new = $this -> data;
			$new['created'] = 	date("Y-m-d H:i:s");
			$new['modified'] = 	date("Y-m-d H:i:s");	
			$new['lastlogin'] = 	date("Y-m-d H:i:s");
			//$new['active'] = 1;			
			if ($this -> User -> save($new)) {
				$this -> Session -> setFlash('User "' . $new['username'] . '" add successful');
				$this -> redirect(array('action' => 'index'));
			}
			$this -> set('errors', $this -> User -> validationErrors);
		}
		$creusrs = $this->User->find('list', array(
				'fields' => array(
					'id', 'username'
				)
			));
			$this->set('creusrs', $creusrs);		
	}

	/**
	* admin edit method
	*/
	public function admin_edit($id){
		
		$this -> User -> id = $id;
		if (!$this -> User -> exists()) {
			$this -> Session -> setFlash('Invalid user id provided');
			 $this -> redirect(array('action' => 'index'));
		}
		
		$conditions = array("User.id" => $id);
		$data = $this->User->find('first', array('conditions' => $conditions));
		$this -> set("data", $data['User']);
		$this -> set("id",$id);
		if ($this -> request -> is('post')) {
			$update = $this -> data;
			$update['modified'] = 	date("Y-m-d H:i:s");	
			if($this -> User -> save($update)){
				$this->Session->setFlash('User "'.$update['username'].'" update successful');
				$this->redirect(array('action' => 'index'));
			}
			$this -> set('errors', $this -> User -> validationErrors);
		}	
	    $creusrs = $this->User->find('list', array(
				'fields' => array(
					'id', 'username'
				)
			));
			$this->set('creusrs', $creusrs);	
	}
	/**
	* admin delete method
	*/
	
	public function admin_delete($id) {
		
		$conditions = array("User.id" => $id);
		$data = $this->User->find('first', array('conditions' => $conditions));
		parent::createException($data['User']);
		$data['User']['active'] = 0;
		
		if($this -> User -> save($data['User'])){
		$this->Session->setFlash('Xóa thành công');
		}else{
			$this->Session->setFlash('Xóa không thành công');
		}
		return $this->redirect(array('controller' => 'Users', 'action' => 'index'));

	}

}
?>